<?
/*

    This file is part of Blue Violin.

    Blue Violin is free software: you can redistribute it and/or modify
    it under the terms of the GNU Affero Public License as published by
    the Free Software Foundation, either version 3 of the License, or
    (at your option) any later version.

    Blue Violin is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU Affero Public License for more details.

    You should have received a copy of the GNU Affero Public License
    along with Blue Violin.  If not, see <http://www.gnu.org/licenses/>.

    Blue Violin  Copyright (C) 2008  Blue Violin, Inc. & Josh Watts <josh.watts@blueviolin.org>
	For more information, visit http://www.blueviolin.org/

    This program comes with ABSOLUTELY NO WARRANTY.

*/
require_once("Database.php");
require_once("Utility.php");
require_once("Security.php");

class BVReports
{

	var $m_oDatabase;
	var $m_oSecurity;
	
	function BVReports()
	{
		$this->m_oDatabase = new Database();
		$this->m_oSecurity = new Security();
	}

	function Destruct()
	{
		$this->m_oDatabase->Destruct();
		$this->m_oSecurity->Destruct();
	}


	function SaveReport($sResult, $sReportJson)
	{
		if (false == $this->m_oSecurity->CheckCookie())
		{
			return;
		}
		$nLoginID = $this->m_oSecurity->GetLoginID();

		$sSQL = "insert into Reports (login_id, report, timestamp, report_guid, result) values " .
			"(" . $nLoginID . ",'" .
			$this->m_oDatabase->EscapeString($sReportJson) . "'," .
			"now(), uuid(), " .
			(("true" == $sResult) ? 1 : 0) . ")";
		$this->m_oDatabase->ExecuteSQL($sSQL);
		return;
	}


	function GetReport($sGUID)
	{
		if (false == $this->m_oSecurity->CheckCookie())
		{
			return;
		}
		$nLoginID = $this->m_oSecurity->GetLoginID();
		$sSQL = "select report " .
			"from Reports " .
			"where login_id = " . $nLoginID . " " .
			"and report_guid = '" . $sGUID . "'";
		$nRows = $this->m_oDatabase->ExecuteSQL($sSQL);
		$aRow = $this->m_oDatabase->NextRow();
		if (get_magic_quotes_gpc())
		{
			$sJson = stripslashes($aRow["report"]);
		}
		else
		{
			$sJson = $aRow["report"];
		}
		return $sJson;
	}
	
	
	function GetHistoricalReportList()
	{
		if (false == $this->m_oSecurity->CheckCookie())
		{
			return "";
		}
		$nLoginID = $this->m_oSecurity->GetLoginID();
		$sJson = "";
		$sSQL = "select report_guid, date_format(timestamp, '%c-%e-%Y %H:%i:%S') as timestamp_formatted, result " .
			"from Reports " .
			"where login_id = " . $nLoginID . " " .
			"order by timestamp desc";
		$nRows = $this->m_oDatabase->ExecuteSQL($sSQL);
		for ($i = 0;
			 $i < $nRows;
			 $i++)
		{
			$aRow = $this->m_oDatabase->NextRow();
			if (0 != $i)
			{
				$sJson .= ",";
			}
			if (null == $aRow["result"])
			{
				$aRow["result"] = 0;
			}
			
			$sJson .= "{\"timestamp\":\"" . $aRow["timestamp_formatted"] . "\"," .
				"\"report_guid\":\"" . $aRow["report_guid"] . "\"," .
				"\"result\":" . $aRow["result"] . "}";
		}
		return "[" . $sJson . "]";
	}

}

?>